# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_compiler=clang

_realname=flang
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_pkgver=20.1.5
pkgver=${_pkgver/-/}
pkgrel=1
pkgdesc="Fortran frontend for LLVM (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://flang.llvm.org/"
msys2_references=(
  "cpe: cpe:/a:llvm:llvm"
  "aur: flang"
  "gentoo: "
)
license=("spdx:Apache-2.0 WITH LLVM-exception")
provides=($([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-fc"))
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-mlir")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "gawk"
             $([[ "$_compiler" == "clang" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-compiler-rt" \
               "${MINGW_PACKAGE_PREFIX}-libc++")
             $([[ "$_compiler" == "gcc" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-gcc")
             )
optdepends=("${MINGW_PACKAGE_PREFIX}-llvm-openmp")
_pkgfn=${_realname}-${_pkgver}.src
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${_pkgver}
source=("${_url}/${_pkgfn}.tar.xz"{,.sig}
        "${_url}/cmake-${_pkgver}.src.tar.xz"{,.sig}
        "0001-fold-double-bessel-functions-on-windows.patch"
        "0002-cmake-22162.patch"
        "0004-do-not-define-pid_t-on-mingw.patch"
        "0005-Fix-c_long_double-value-on-mingw-aarch64.patch"
        "0007-add-municode-flag-on-mingw.patch"
        "0008-do-not-use-clock_gettime-on-mingw.patch")
sha256sums=('f2bb960ce4d6356721d93e46e204628aeaa7d9f7d8a1998d091adbd779929406'
            'SKIP'
            '1b5abaa2686c6c0e1f394113d0b2e026ff3cb9e11b6a2294c4f3883f1b02c89c'
            'SKIP'
            'ae666c454909f6695bcd69f067b9c5abf8a6e82ce2930955b8598ab3b58a5204'
            '77fb0612217b6af7a122f586a9d0d334cd48bb201509bf72e8f8e6244616e895'
            '90bec8f2a9a8d62b57959722f3b15a54dfdaf24e0a5ea884bf6b1288d0853be8'
            'a1811227e2a26c2786b0c8577528580e679111f6c0889afea57d08333e2d5e4f'
            '45f8da68b2cb48c85db7099e174e7eebeabe90207a23ff19c7fe1e7f1f8541d8'
            'be1265d4922f45cc4e7ec6a80247baba8b1e648e1e514718ebd1004958bae967')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  [[ -d ./cmake ]] && rm -rf ./cmake
  mv cmake-${_pkgver}.src cmake
  cd "${srcdir}/flang-${_pkgver}.src"

  apply_patch_with_msg \
    "0001-fold-double-bessel-functions-on-windows.patch" \
    "0002-cmake-22162.patch" \
    "0004-do-not-define-pid_t-on-mingw.patch" \
    "0005-Fix-c_long_double-value-on-mingw-aarch64.patch" \
    "0007-add-municode-flag-on-mingw.patch" \
    "0008-do-not-use-clock_gettime-on-mingw.patch"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [ "${_compiler}" == "clang" ]; then
    export CC='clang'
    export CXX='clang++'
    _extra_config+=(-DLLVM_ENABLE_LLD=ON)
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_THREADS=ON \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DCLANG_DIR=${MINGW_PREFIX}/lib/cmake/clang \
    -DLLVM_DIR=${MINGW_PREFIX}/lib/cmake/llvm \
    -DMLIR_DIR=${MINGW_PREFIX}/lib/cmake/mlir \
    -DFLANG_INCLUDE_TESTS=OFF \
    "${_extra_config[@]}" \
    -Wno-dev \
    ../${_pkgfn}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake.exe -DFLANG_INCLUDE_TESTS=ON ../${_pkgfn}
  ${MINGW_PREFIX}/bin/cmake.exe --build .
  ${MINGW_PREFIX}/bin/cmake.exe --build . -- check-flang || true
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  install -Dm644 "${srcdir}"/${_pkgfn}/LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
